package sg.nus.iss.vmcs.gui.elements;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.event.ActionListener;

import sg.nus.iss.vmcs.controllers.CoinReceiver;
import sg.nus.iss.vmcs.guilisteners.CoinInputListener;
import sg.nus.iss.vmcs.models.Coin;
import sg.nus.iss.vmcs.utils.Constant;
import sg.nus.iss.vmcs.utils.MoneyFormatter;

/**
 * Coin Input Box used in the Customer Panel
 * @author Philip
 */
public class CoinInputBox extends Panel {

	/** Generated Serial ID */
	private static final long serialVersionUID = -2149835742892182769L;
	
	/** Button array for the list of buttons */
	private Button[] 	btnCoinInputArray;
	
	/** Panel Holding the buttons together */
	private Panel		pnlButtonHolder;
	
	/** Label for the title of the Coin Input Box */
	private Label		lblTitle;
	
	/**
	 * Creates an instance of the CoinInputBox
	 * @param coinReceiver Reference to the controller for the CoinInputBox
	 */
	public CoinInputBox(CoinReceiver coinReceiver) {
		
		//Title Label		
		lblTitle = new Label("Enter Coins Here: ");
		lblTitle.setFont(Constant.LABEL_FONT);
		
		//Button Panel
		Coin[] stdCoins 	= coinReceiver.getCoinRegistry().getAllCoins();
		btnCoinInputArray 	= new Button[stdCoins.length + 1];
		pnlButtonHolder 	= new Panel();
		ActionListener cl	= new CoinInputListener(coinReceiver);
		pnlButtonHolder.setLayout(new GridLayout(1, btnCoinInputArray.length));
		
		//Adding Valid Buttons
		for(int i = 0; i < btnCoinInputArray.length - 1; i++) {
			btnCoinInputArray[i] = new Button(MoneyFormatter.toMoney(stdCoins[i].getValue()));
			btnCoinInputArray[i].setActionCommand(String.valueOf(stdCoins[i].getWeight()));
			btnCoinInputArray[i].addActionListener(cl);
			pnlButtonHolder.add(btnCoinInputArray[i]);
		}
		
		//Adding Invalid Button
		Button invalidButton = new Button("Invalid");
		invalidButton.setActionCommand("9999999");
		invalidButton.addActionListener(cl);
		btnCoinInputArray[btnCoinInputArray.length - 1] = invalidButton;
		pnlButtonHolder.add(invalidButton);
		
		this.setLayout(new BorderLayout());
		this.add(lblTitle, BorderLayout.NORTH);
		this.add(pnlButtonHolder, BorderLayout.CENTER);
	}
	
	/**
	 * Sets the buttons to be enabled
	 * @param enabled true to enable, false if otherwise
	 */
	public void setActive(boolean enabled) {
		for(Button b : btnCoinInputArray) {
			b.setEnabled(enabled);
		}
	}
}
